//cookie
var cindex1, cindex2, cindex11, cindex22;

cindex11 = $.cookie("cindex11");
cindex22 = $.cookie("cindex22");

var expiresDate = new Date();
expiresDate.setTime(expiresDate.getTime() + (60 * 60 * 1000));

$(".b1r-left li").click(function() {
	cindex1 = $(this).index();
	$.cookie("cindex11", cindex1, {
		path: '/',
		expires: expiresDate
	});
	tab1(cindex1);
	cindex22 = 0;
	$.cookie("cindex22", cindex22, {
		path: '/',
		expires: expiresDate
	});
});

function tab1(index) {
	$(".b1r-left li").eq(index).addClass("liAc").siblings(".b1r-left li").removeClass("liAc");
	sourceId = $(".b1r-left li").eq(index).attr("value");

	currentPage = 1;
	$(".search").val("");
	keyWord = $(".search").val();
	emHide();
	$(".zxf_pagediv").hide();

	getCagy();
}

//category
var sourceId;

if(cindex11 == undefined) {
	sourceId = $(".b1r-left li").eq(0).attr("value");
	$(".b1r-left li").eq(0).addClass("liAc").siblings(".b1r-left li").removeClass("liAc");
} else {
	sourceId = $(".b1r-left li").eq(cindex11).attr("value");
	$(".b1r-left li").eq(cindex11).addClass("liAc").siblings(".b1r-left li").removeClass("liAc");
}

getCagy();

function getCagy() {
	ajaxPost(LIST_CATEGORY_URL, {
		"sourceId": sourceId,
	}, getCsucc, ajerror, 15000);
}

function getCsucc(data) {
	var error_code = data.code;
	var error_msg = data.msg;
	$(".b1r-right ul").html("");

	if(error_code == 1) {

		var data = data.data;

		for(var i = 0; i < data.length; i++) {
			var lis = '<li class="txtclass" sourceId=' + data[i].source + '>' + data[i].name + '</li>';
			$(".b1r-right ul").append(lis);
		}

		if(cindex22 == undefined) {
			categoryName = $(".b1r-right ul li").eq(0).html();
			$(".b1r-right li").eq(0).addClass("liAc").siblings(".b1r-right li").removeClass("liAc");
		} else {
			categoryName = $(".b1r-right ul li").eq(cindex22).html();
			$(".b1r-right li").eq(cindex22).addClass("liAc").siblings(".b1r-right li").removeClass("liAc");
		}

		getList(currentPage, everyPage, sourceId, categoryName, keyWord);

		$(".b1r-right li").click(function() {
			cindex2 = $(this).index();
			$.cookie("cindex22", cindex2, {
				path: '/',
				expires: expiresDate
			});
			tab2(cindex2)
		});

	} else {
		check_code(error_msg);
	}
}

//

function tab2(index) {
	categoryName = $(".b1r-right li").eq(index).html();
	$(".b1r-right li").eq(index).addClass("liAc").siblings(".b1r-right li").removeClass("liAc");

	currentPage = 1;

	$(".search").val("");
	keyWord = $(".search").val();
	emHide();
	$(".zxf_pagediv").hide();
	getList(currentPage, everyPage, sourceId, categoryName, keyWord);
}

//list

var currentPage = 1;
var totalPage, categoryName, keyWord;
var everyPage = 10;
var page_type = "category_list";

function getList(current, everyp, id, name, keyw) {
	diaLoding("数据加载中...");
	ajaxPost(LIST_LIST_URL, {
		"currentPage": current,
		"totalCount": everyp,
		"sourceId": id,
		"category": name,
		"keyword": keyw,
	}, getLsucc, ajerror, 300000);
}

function getLsucc(data) {
	diaLodend();
	var error_code = data.code;
	var error_msg = data.msg;
	var data1, list;
	$(".table tr:gt(0)").remove();
	if(error_code == 1) {

		var data1 = data.data;
		if(currentPage == 1) {
			totalPage = data1.pageCount;
		}

		if(totalPage == 0) {
			emShow("该类别下数据为空...");
		} else if(totalPage == 1) {
			emHide();
			$(".zxf_pagediv").hide();
			list = data1.list;
			getLcontent(list);
		} else {
			emHide();
			$(".zxf_pagediv").show();
			list = data1.list;
			getLcontent(list);
			$(".zxf_pagediv").createPage({
				pageNum: totalPage,
				current: currentPage,
				shownum: 9,
			});
		}

	} else {
		check_code(error_msg);
	}
}

function getLcontent(list) {
	for(var i = 0; i < list.length; i++) {
		var tds = '<tr Aurl="' + list[i].url + '">' +
			'<td class="t-title" title="' + list[i].title + '">' + list[i].title + '</td>' +
			'<td>' + list[i].author + '</td>' +
			'<td class="original" style="display:none">' + list[i].oriAuthor + '</td>' +
			'<td class="original" style="display:none" title="' + list[i].oriUrl + '"><a target="_bank" class="txtclass" href="' + list[i].oriUrl + '">' + list[i].oriUrl + '</a></td>' +
			'<td>' + list[i].category + '</td>' +
			'<td>' + list[i].commentCount + '</td>' +
			'<td>' + list[i].publishTime + '</td>' +
			'<td>' +
			'<a class="t-view btnclass btnhover">查看</a>' +
			'</td>' +
			'</tr>';
		$(".table").append(tds);
	}

	$(".original a").each(function() {
		if($(this).text().length > 12) {
			$(this).text($(this).text().slice(0, 11) + "...");
		}
	});

	$(".t-view").each(function() {
		$(this).click(function() {
			var aurl = Encrypt($(this).parents("tr").attr("Aurl"));
			var atitle = Encrypt($(this).parents("tr").find(".t-title").attr("title"));
			var aId = Encrypt(sourceId);
			if(aurl == "" || aurl == undefined || aurl == null) {
				diaMsg("链接不存在", 2);
			} else {
				window.location.href = "details.html?u=" + aurl + "&&t=" + atitle + "&&i=" + aId;
			}
		});
	});

	if(sourceId == 2 || sourceId == 3) {
		$(".original").hide();
	} else if(sourceId == 1) {
		$(".original").show();
	}
}

$(".submit1").click(function() {
	getSearch()
});

$(document).keydown(function(event) {
	if(event.keyCode == 13) {
		getSearch();
	}
});

function getSearch() {
	keyWord = $(".search").val();
	currentPage = 1;

	emHide();
	$(".zxf_pagediv").hide();
	getList(currentPage, everyPage, sourceId, categoryName, keyWord);
}